import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/sales/api/v1';

const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: 'KD整车发运单'
});

const getInitDesc = formatMessage({
    id: 'api.getInit',
    defaultMessage: '初始化'
});

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const getAddDesc = formatMessage({
    id: 'api.getAdd',
    defaultMessage: '新增{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const getUpdateDesc = formatMessage({
    id: 'api.getUpdate',
    defaultMessage: '修改{resourceName}'
}, {resourceName});

const getAbandonDesc = formatMessage({
    id: 'api.getAbandon',
    defaultMessage: '作废'
});

const getSignnedDesc = formatMessage({
    id: 'api.getSignnedDesc',
    defaultMessage: '签收'
});

const getDatesupplementDesc = formatMessage({
    id: 'api.getDatesupplementDesc',
    defaultMessage: '日期补录'
});

const getSalePackingListsDesc = formatMessage({
    id: 'api.getSalePackingListsDesc',
    defaultMessage: '已开票包装箱单查询'
});

const exportDesc = formatMessage({
    id: 'api.exportDesc',
    defaultMessage: '导出'
});

// 初始化请求
export const getInitData = () =>
    request(`${baseUrl}/ui/KDSaleShipment/init`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitDesc));

// 查询KD整车发运单
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/KDSaleShipments?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 新增KD整车发运单
export const newOrder = data => request(`${baseUrl}/KDSaleShipments`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAddDesc));

// 查询KD整车发运单详情
export const getDetail = id => request(`${baseUrl}/KDSaleShipments/${id}`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 修改KD整车发运单
export const updateOrder = (id, data) => request(`${baseUrl}/KDSaleShipments/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getUpdateDesc));

// 作废
export const abandonData = (id, rowVersion) => request(`${baseUrl}/KDSaleShipments/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(rowVersion)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAbandonDesc));

// 签收
export const signnedCondition = (id, data) => request(`${baseUrl}/KDSaleShipments/${id}/signned`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getSignnedDesc));

// 日期补录
export const datesupplementCondition = (id, data) => request(`${baseUrl}/KDSaleShipments/${id}/datesupplement`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getDatesupplementDesc));

// 包装箱单零件查询
export const getSalePackingLists = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/KDSaleShipments/salePackingListPart?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getSalePackingListsDesc));
};

// 导出
export const exports = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/KDSaleShipments/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};
